package com.datastructure.test.findcontinuoussequenceequals;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class FindContinuousSequenceEqualS {

    public static void main(String[] args) {
        FindContinuousSequenceEqualS findContinuousSequenceEqualS = new FindContinuousSequenceEqualS();
        ArrayList<Integer> integers = findContinuousSequenceEqualS.orderArray(20);
        System.out.println(integers);
    }

    public ArrayList<ArrayList<Integer>> FindContinuousSequence(int sum) {
        ArrayList<ArrayList<Integer>> data = new ArrayList<>();
        int length = sum/2+1;
        int sumTemp;
        int j;
        ArrayList<Integer> tempList;
        for (int i = 1; i < length; i++) {
            sumTemp=0;
            tempList=new ArrayList<>();
            j=i;
            while (sumTemp<sum){
                sumTemp+=j;
                tempList.add(j);
                j++;
            }
            if(sumTemp==sum){
                data.add(tempList);
            }
        }
        return data;
    }

    public ArrayList<Integer> orderArray (int n) {
        // write code here
        ArrayList<String> data = new ArrayList<>(n);
        for (int i = 1; i <= n; i++) {
            data.add(String.valueOf(i));
        }
        return (ArrayList<Integer>)data.stream().sorted(Comparator.comparingInt(s -> s.charAt(0))).map(Integer::parseInt).collect(Collectors.toList());
    }
}
